﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Super.Common.Validate
{
    /// <summary> 数据验证规则 
    /// </summary>    
    public class ValidateRules
    {
        public ValidateRules(string controlName)
        {
            if (controlName == null)
            {
                throw new Exception("Name 不能为空!");
            }

            ControlName = controlName;
            ValidateElementList = new List<ValidateElement>();
        }

        /// <summary> 对控件进行数据验证
        /// </summary>
        /// <param name="resultMsg"></param>
        /// <returns></returns>
        internal bool Check(out string resultMsg)
        {
            string value = System.Web.HttpContext.Current.Request.Form[ControlName];

            resultMsg = null;

            if (value != null)
            {
                foreach (var item in ValidateElementList)
                {
                    if (item.Type != "required" && value == "")
                    {
                        return true;
                    }


                    /*
                    if (item.ValidateMethodByMultiParameter != null)
                    {
                        //多参数验证
                        if (!item.ValidateMethodByMultiParameter.Invoke(System.Web.HttpContext.Current.Request))
                        {
                            resultMsg = item.ErrorMessage;
                            return false;
                        }
                    }
                     */

                    //远端验证需要类型转换.
                    if (item.ValidateMethodByRemote != null)
                    {
                        var rt = item.ValidateMethodByRemote.Invoke(value).ToString();

                        if (rt == "True")
                        {
                            return true;
                        }
                        else
                        {

                            resultMsg = rt.ToString();
                            return false;
                        }
                    }
                    //单参数验证
                    if (!item.ValidateMethod.Invoke(value, item.Parameter))
                    {
                        var parm = item.Parameter.Trim("[]".ToCharArray()).Split(',');
                        resultMsg = string.Format(item.ErrorMessage, parm);
                        return false;
                    }
                }
            }

            return true;
        }

        /// <summary> 对控件进行数据验证
        /// </summary> 
        /// <returns></returns>
        internal bool Check()
        {
            string s = "";
            return Check(out s);
        }

        /// <summary> 控件Name
        /// </summary>
        public string ControlName { get; private set; }

        /// <summary> 规则中包含的验证元素集合
        /// </summary>
        public List<ValidateElement> ValidateElementList { get; set; }

        /// <summary> 获取JQueryValidateString
        /// </summary>
        public string GetJQueryValidateString()
        {
            var validate = new System.Text.StringBuilder("validate=\"{");
            var message = new System.Text.StringBuilder();

            foreach (var item in this.ValidateElementList)
            {
                validate.Append(item.Type + ":" + item.Parameter + ",");
                message.Append(item.Type + ":'" + item.ErrorMessage + "',");
            }

            return validate.Append("messages:{").Append(message.Remove(message.Length - 1, 1)).Append("}}\"").ToString();

        }
    }
}
